package com.zt.student.service.impl;

import com.zt.common.Do.ActiveClassDO;
import com.zt.common.Do.PrivateLetterDO;
import com.zt.common.Do.PrivateLetterWithInfoDO;
import com.zt.common.pojo.PrivateLetter;
import com.zt.common.pojo.Teachers;
import com.zt.common.pojo.netty.Sender;
import com.zt.common.utils.JwtUtil;
import com.zt.student.dao.PrivateLetterDao;
import com.zt.student.dto.PrivateLetterDetailDTO;
import com.zt.student.service.ActiveClassService;
import com.zt.student.service.ClassSelectionService;
import com.zt.student.service.PrivateLetterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * description:
 * author:zt
 * date:2022-02-06
 */
@Service
@Transactional
public class PrivateLetterServiceImpl implements PrivateLetterService {

    @Autowired
    private PrivateLetterDao privateLetterDao;
    @Autowired
    private ClassSelectionService classSelectionService;
    @Autowired
    private ActiveClassService activeClassService;

    @Override
    public ArrayList<PrivateLetterWithInfoDO> getRecord(HttpServletRequest request, String semester){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));
        ArrayList<PrivateLetterWithInfoDO> list = new ArrayList<>();

        //获取当前学生关联的所有的老师信息
        List<Sender> teacherInfo = classSelectionService.getSelectedTeacherInfo(request,semester);
        for (Sender teacher : teacherInfo) {
            //获取当前用户与该老师最近10条消息
            List<PrivateLetter> letterLists = privateLetterDao.select(studentId, teacher.getId());
            //如果没有聊天记录则跳过
            if (letterLists == null || letterLists.size()==0) continue;

            ArrayList<ActiveClassDO> activeClassDOS = new ArrayList<>();
            activeClassService.getClassByTeaAndStuId(request,teacher.getId(),semester).forEach(a -> {
                activeClassDOS.add(new ActiveClassDO(a.getId(),a.getName()));
            });

            PrivateLetterWithInfoDO privateLetterWithInfoDO = new PrivateLetterWithInfoDO();
            privateLetterWithInfoDO.setUserId(teacher.getId()).setName(teacher.getName()).setAvatar(teacher.getAvatar()).setActiveClassDOS(activeClassDOS);

            ArrayList<PrivateLetterDO> privateLetterDOS = new ArrayList<>();

            letterLists.forEach(l -> {
                if (l.getSenderId().equals(studentId))
                    //自己是发送方
                    privateLetterDOS.add(new PrivateLetterDO(l.getMessage(),l.getSendTime(),0,l.getStatus()));
                else
                    //自己是接收方
                    privateLetterDOS.add(new PrivateLetterDO(l.getMessage(),l.getSendTime(),1,l.getStatus()));
            });
            privateLetterWithInfoDO.setPrivateLetterDOS(privateLetterDOS);
            list.add(privateLetterWithInfoDO);
        }

        list.sort(new Comparator<PrivateLetterWithInfoDO>() {
            DateFormat f = new SimpleDateFormat("yyyy-MM-dd");

            @Override
            public int compare(PrivateLetterWithInfoDO a, PrivateLetterWithInfoDO b) {
                return b.getPrivateLetterDOS().get(0).getSendTime().compareTo(a.getPrivateLetterDOS().get(0).getSendTime());
            }
        });
        return list;
    }

    /**
     * @return 返回消息发送时间
     */
    @Override
    public String insert(PrivateLetter privateLetter,HttpServletRequest request){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));

        Date date = new Date();
        privateLetter.setSenderId(studentId).setSendTime(date).setStatus(0);
        privateLetterDao.insert(privateLetter);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    @Override
    public List<PrivateLetterDO> getDetail(HttpServletRequest request, PrivateLetterDetailDTO privateLetterDetailDTO){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));

        List<PrivateLetter> detail = privateLetterDao.getDetail(studentId, privateLetterDetailDTO.getUserId(), privateLetterDetailDTO.getTime());
        ArrayList<PrivateLetterDO> list = new ArrayList<>();

        for (PrivateLetter letter : detail) {
            list.add(new PrivateLetterDO(letter.getMessage(), letter.getSendTime(), studentId.equals(letter.getSenderId())?0:1, letter.getStatus()));
        }
        return list;
    }

    @Override
    public void readLetter(HttpServletRequest request,PrivateLetterDetailDTO privateLetterDetailDTO){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));

        privateLetterDao.readLetter(privateLetterDetailDTO.getUserId(), studentId, privateLetterDetailDTO.getTime());
    }

    /**
     * @return 获取该用户的所有未读消息数量
     */
    @Override
    public int getUnreadCount(HttpServletRequest request, String semester){
        String studentId = JwtUtil.decodedGetID(request.getHeader("Authorization"));
        return privateLetterDao.getUnreadCount(studentId,semester);
    }
}
